package com.ljc.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljc.entity.Category;
import com.ljc.service.CategoryService;
import com.ljc.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * 项目启动/标题自动获取
 */
@Component
public class ContextStartup implements ApplicationRunner, ServletContextAware {
    @Autowired
    private CategoryService categoryService;

    ServletContext servletContext;
    @Autowired
    PostService postService;

    /**
     * @Author: ljc
     * @Description:ApplicationRunner项目启动调用run方法返回数据
     * @Date:Created in 18:45 2021/12/4
     * @Modified By:
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //获取标题集合返回渲染到前端
        List<Category> categories = categoryService.list(new QueryWrapper<Category>()
                .eq("status", 0));
        System.out.println(categories);
        servletContext.setAttribute("categorys", categories);
        //本周热议初始化
        postService.initWeekRank();
    }

    /**
     * @Author: ljc
     * @Description:
     * @Date:Created in 18:47 2021/12/4
     * @Modified By:
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
